﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using 基于WPF的电缆采样数据分析系统.MsgEvents.DeviceStatus;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.DeviceStatus
{
    public class TimeRegionUCViewModel : BindableBase
    {

        // 单例模型
        public static TimeRegionUCViewModel Instance = new TimeRegionUCViewModel(App.GetService<IRegionManager>(), App.GetService<IRegionNavigationJournal>());

        /// <summary>
        /// 区域管理器
        /// </summary>
        private readonly IRegionManager RegionManager;

        /// <summary>
        /// 历史记录
        /// </summary>
        private IRegionNavigationJournal Journal;

        /// <summary>
        /// 发布订阅
        /// </summary>
        private readonly IEventAggregator Aggregator;

        /// <summary>
        /// 构造器
        /// </summary>
        public TimeRegionUCViewModel(IRegionManager _RegionManager, IRegionNavigationJournal _Journal)
        {
            RegionManager = _RegionManager;

            Journal = _Journal;

            // 初始化调度程序定时器
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += Timer_Tick;
            _timer.Start();

            Aggregator = App.GetService<IEventAggregator>();
            // 导入波形次数
            Aggregator.GetEvent<MsgImportWaveformCountCompute>().Subscribe(ImportWaveformCountCompute);
            // 创建工程次数
            Aggregator.GetEvent<MsgCreateProjectCountCompute>().Subscribe(CreateProjectCountCompute);
            // 导出波形次数
            Aggregator.GetEvent<MsgExportWaveformCountCompute>().Subscribe(ExportWaveformCountCompute);
                

            // 绑定导航到设置界面命令
            SettingsCmm = new DelegateCommand(SettingsFun);
        }


        #region 时间

        /// <summary>
        /// 时分秒
        /// </summary>
        private string _Hms = DateTime.Now.ToString("HH:mm:ss");

        /// <summary>
        /// 时分秒
        /// </summary>
        public string Hms
        {
            get { return _Hms; }
            set
            {
                _Hms = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 年月日
        /// </summary>
        private string _YearToDate = DateTime.Now.ToString("yyyy-MM-dd");

        /// <summary>
        /// 年月日
        /// </summary>
        public string YearToDate
        {
            get { return _YearToDate; }
            set
            {
                _YearToDate = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 星期
        /// </summary>
        private string _Week = DateTime.Now.ToString("dddd");

        /// <summary>
        /// 星期
        /// </summary>
        public string Week
        {
            get { return _Week; }
            set
            {
                _Week = value;
                RaisePropertyChanged();
            }
        }


        #endregion


        #region 导入波形次数

        private string _DRBX_One = "0";

        public string DRBX_One
        {
            get { return _DRBX_One; }
            set
            {
                _DRBX_One = value;
                RaisePropertyChanged();
            }
        }


        private string _DRBX_Two = "0";

        public string DRBX_Two
        {
            get { return _DRBX_Two; }
            set
            {
                _DRBX_Two = value;
                RaisePropertyChanged();
            }
        }


        private string _DRBX_Three = "0";

        public string DRBX_Three
        {
            get { return _DRBX_Three; }
            set
            {
                _DRBX_Three = value;
                RaisePropertyChanged();
            }
        }


        private string _DRBX_Four = "0";

        public string DRBX_Four
        {
            get { return _DRBX_Four; }
            set
            {
                _DRBX_Four = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 导入波形次数更新
        /// </summary>
        private void ImportWaveformCountCompute()
        {
            var ICount = GlobalVariableEdit.ImportWaveformCount;

            if (ICount >= 0 && ICount <= 9)
            {
                DRBX_Four = ICount.ToString();
            } else if (ICount >= 10 && ICount <= 99)
            {
                DRBX_Four = (ICount % 10).ToString();
                DRBX_Three = (ICount / 10).ToString();
            } else if (ICount >= 100 && ICount <= 999)
            {
                DRBX_Four = (ICount % 10).ToString();
                DRBX_Three = (ICount / 10).ToString();
                DRBX_Two = (ICount / 100).ToString();
            } else if (ICount >= 1000 && ICount <= 9999)
            {
                DRBX_Four = (ICount % 10).ToString();
                DRBX_Three = (ICount / 10).ToString();
                DRBX_Two = (ICount / 100).ToString();
                DRBX_One = (ICount / 1000).ToString();
            }
        }


        #endregion


        #region 新建工程次数

        private string _XJGC_One = "0";

        public string XJGC_One
        {
            get { return _XJGC_One; }
            set
            {
                _XJGC_One = value;
                RaisePropertyChanged();
            }
        }


        private string _XJGC_Two = "0";

        public string XJGC_Two
        {
            get { return _XJGC_Two; }
            set
            {
                _XJGC_Two = value;
                RaisePropertyChanged();
            }
        }


        private string _XJGC_Three = "0";

        public string XJGC_Three
        {
            get { return _XJGC_Three; }
            set
            {
                _XJGC_Three = value;
                RaisePropertyChanged();
            }
        }


        private string _XJGC_Four = "0";

        public string XJGC_Four
        {
            get { return _XJGC_Four; }
            set
            {
                _XJGC_Four = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 更新创建工程次数
        /// </summary>
        private void CreateProjectCountCompute()
        {
            var CCount = GlobalVariableEdit.CreateWaveformCount;

            if (CCount >= 0 && CCount <= 9)
            {
                XJGC_Four = (CCount % 10).ToString();
            }
            else if (CCount >= 10 && CCount <= 99)
            {
                XJGC_Four = (CCount % 10).ToString();
                XJGC_Three = (CCount / 10).ToString();
            }
            else if (CCount >= 100 && CCount <= 999)
            {
                XJGC_Four = (CCount % 10).ToString();
                XJGC_Three = (CCount / 10).ToString();
                XJGC_Two = (CCount / 100).ToString();
            }
            else if (CCount >= 1000 && CCount <= 9999)
            {
                XJGC_Four = (CCount % 10).ToString();
                XJGC_Three = (CCount / 10).ToString();
                XJGC_Two = (CCount / 100).ToString();
                XJGC_One = (CCount / 1000).ToString();
            }
        }

        #endregion


        #region 导出波形次数

        private string _DCBX_One = "0";

        public string DCBX_One
        {
            get { return _DCBX_One; }
            set
            {
                _DCBX_One = value;
                RaisePropertyChanged();
            }
        }


        private string _DCBX_Two = "0";

        public string DCBX_Two
        {
            get { return _DCBX_Two; }
            set
            {
                _DCBX_Two = value;
                RaisePropertyChanged();
            }
        }


        private string _DCBX_Three = "0";

        public string DCBX_Three
        {
            get { return _DCBX_Three; }
            set
            {
                _DCBX_Three = value;
                RaisePropertyChanged();
            }
        }


        private string _DCBX_Four = "0";

        public string DCBX_Four
        {
            get { return _DCBX_Four; }
            set
            {
                _DCBX_Four = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 更新导出波形次数
        /// </summary>
        private void ExportWaveformCountCompute()
        {
            var ECount = GlobalVariableEdit.ExportWaveformCount;

            if (ECount >= 0 && ECount <= 9)
            {
                DCBX_Four = (ECount % 10).ToString();
            }
            else if (ECount >= 10 && ECount <= 99)
            {
                DCBX_Four = (ECount % 10).ToString();
                DCBX_Three = (ECount / 10).ToString();
            }
            else if (ECount >= 100 && ECount <= 999)
            {
                DCBX_Four = (ECount % 10).ToString();
                DCBX_Three = (ECount / 10).ToString();
                DCBX_Two = (ECount / 100).ToString();
            }
            else if (ECount >= 1000 && ECount <= 9999)
            {
                DCBX_Four = (ECount % 10).ToString();
                DCBX_Three = (ECount / 10).ToString();
                DCBX_Two = (ECount / 100).ToString();
                DCBX_One = (ECount / 1000).ToString();
            }
        }


        #endregion


        #region 导航到设置界面

        /// <summary>
        /// 导航到设置界面命令
        /// </summary>
        public DelegateCommand SettingsCmm { get; set; }

        /// <summary>
        /// 导航到设置界面方法
        /// </summary>
        private void SettingsFun()
        {
            RegionManager.Regions["MainViewRegion"].RequestNavigate("SettingsUC",
                callback =>
                {
                    Journal = callback.Context.NavigationService.Journal; // 记录导航足迹
                });

            Aggregator.GetEvent<MsgNavicationSetting>().Publish();
        }
        #endregion


        #region 时间显示
        /// <summary>
        /// 定义调度程序定时器
        /// </summary>
        private readonly DispatcherTimer _timer;

        /// <summary>
        /// 时间刷新
        /// </summary>
        private void Timer_Tick(object sender, EventArgs e)
        {
            Hms = DateTime.Now.ToString("HH:mm:ss");
            YearToDate = DateTime.Now.ToString("yyyy-MM-dd");

            if (GlobalVariableEdit.LangugeStatus == LangugeType.Chinese)
            {
                Week = DateTime.Now.ToString("dddd", CultureInfo.CreateSpecificCulture("zh-CN"));
            }
            else
            {
                Week = DateTime.Now.ToString("dddd", CultureInfo.CreateSpecificCulture("en-US"));
            }

        }
        #endregion
    }
}
